function selectSupplier() {
	// iframe层
	layer.open({
		type : 2,
		title : "选择供应商",
		closeBtn : "1",
		shadeClose : true,
		btn : [ '选择', '取消' ],
		shade : [ 0 ],
		shift : 2,
		area : [ '65%', '85%' ],
		content : basePath+'/userInfo/selectUser.do',
		yes : function(index, layero) {

			var iframeWin = window[layero.find('iframe')[0]['name']]; // 得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();

			iframeWin.returnSupplierForFI();

			layer.closeAll();

		},
		btn2 : function() {
			layer.closeAll();
		}

	});
}
function selectDemand() {
	// iframe层
	layer.open({
		type : 2,
		title : "选择订单",
		closeBtn : "1",
		shadeClose : true,
		btn : [ '选择', '取消' ],
		shade : [ 0 ],
		shift : 2,
		area : [ '95%', '90%' ],
		content : basePath+'/myDemandManager/selectDemandPage.do',
		yes : function(index, layero) {

			var iframeWin = window[layero.find('iframe')[0]['name']]; // 得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();

			iframeWin.returnSupplierForFI();

			layer.closeAll();

		},
		btn2 : function() {
			layer.closeAll();
		}

	});
}


$(document).ready(function(){
	
	renderForm();
	
	$('#save-factory-inspection').on('click',function(){
		
		if(checkorderedit())
			{
			saveFactoryInspection();
			}
	})
	
	
})
function refreshInherit(id){
	$.post(basePath+'/factoryinspection/getFactotyInspctionInfoById.do',{'id':id},function(data){
		
		
		$('#factory-addr').val(data.factoryAddress);
		$('#factory-photoes').val(data.factoryPhotoes);
		$('#manager-name').val(data.factoryManagerName);
		$('#manager-contact').val(data.factoryManagerContact);
		$('#license-photo').val(data.factoryLicensePhoto);
		$('#main-project').val(data.mainProject);

		$('#workroom-area').val(data.workroomArea);
		$('#workman-number').val(data.workmanNumber);
		$('#management').val(data.management);
		$('#overall-evaluation').val(data.overallEvaluation);
		$('#boss-native').val(data.bossNativePlace);
		$('#boss-evaluation').val(data.bossEvaluation);
		$('#other-require').val(data.otherRequire);
		
		if(data.hasEquipment){
			$('#has-equipment').prop('checked',true);
		}
		if(data.qualityTestAlone){
			$('#quality-test-alone').prop('checked',true);
		}
		if(data.lowQualityProcess){
			$('#low-quality-process').prop('checked',true);
		}
		
		var qualityLevel=document.getElementById("produce-quality-level");
		selected(qualityLevel,data.produceQualityLevel);
		
		var costLevel=document.getElementById("process-cost-level");
		selected(costLevel,data.produceQualityLevel);
		
		renderEquipmentForObj(data.equipmentStatus);
		renderTechForObj(data.technicalPersonnel);
		renderProductForObj(data.processProducts);
		renderCategoryForObj(data.processCategory);
		
		renderFactoryPhotoesForObj(data.factoryPhotoes)
		renderLicensePhotoesForObj(data.factoryLicensePhoto);
		
	});		
}

function selected(obj,value){
	for(var i=0;i<obj.length;i++){
		if(obj.options[i].value == value){
			obj.options[i].selected=true;
		}
	}
}

function renderEquipment()
{
	var json=$('#equipment-status').val();
	
	if (json!='')
	{
		json=$.parseJSON(json);
	}
	
	 var tp = $('#equipment-sel');

	    tp.empty();

	    for (var i = 0; i < json.length; i++) {

	        var li = renderEquipmentLi(json[i]);

	        tp.append(li);

	    }
	
		
}

function saveFactoryInspection()
{
	$.post(basePath+'/factoryinspection/save.do',{
		
		"id":$('#id').val(),
		"refClientId":$.trim($('#ref-client-id').val()),
		"orderNo":$.trim($('#order-no').val()),
		"factoryName":$.trim($('#factory-name').val()),
		"factoryAddress":$.trim($('#factory-addr').val()),
		"factoryPhotoes":$.trim($('#factory-photoes').val()),
		"factoryManagerName":$.trim($('#manager-name').val()),
		"factoryManagerContact":$.trim($('#manager-contact').val()),
		"factoryLicensePhoto":$.trim($('#license-photo').val()),
		"mainProject":$.trim($('#main-project').val()),
		"equipmentStatus":getEquipmentValue(),//$.trim($('#equipment-status').val()),
		"processCategory":getCategoryValue(),//$.trim($('#process-category').val()),
		"processProducts":getProductValue(),//$.trim($('#process-product').val()),
		"produceQualityLevel":$.trim($('#produce-quality-level option:selected').val()),
		"processCostLevel":$.trim($('#process-cost-level option:selected').val()),
		"workroomArea":$.trim($('#workroom-area').val()),
		"workmanNumber":$.trim($('#workman-number').val()),
		"technicalPersonnel":getTechValue(),//$.trim($('#technical-personnel').val()),
		"qualityTestAlone":$('#quality-test-alone').prop('checked'),
		"lowQualityProcess":$('#low-quality-process').prop('checked'),
		"management":$.trim($('#management').val()),
		"overallEvaluation":$.trim($('#overall-evaluation').val()),
		"bossNativePlace":$.trim($('#boss-native').val()),
		"bossEvaluation":$.trim($('#boss-evaluation').val()),
		"hasEquipment":$('#has-equipment').prop('checked'),
		"otherRequire":$.trim($('#other-require').val()),
		"orderStatus":$('#order-status option:selected').val(),
		"supplier":$.trim($('#supplier').val())
		
	},function(data){
	
		swal( data.msg, "","success");
		
		if(data.status==10001)
		{
			if($('#id').val()==''&& optionType!='add')
			{
				$('#id').val(data.data);
				
				window.location.href=basePath+'/factoryinspection/edit.do?id='+data.data+'&refClientId='+$.trim($('#ref-client-id').val());
			}
			
			renderForm();
			
			
			var report=window.parent.document.getElementById("report");
			
			if(report!=null){
				
				var date = new Date();
			    var seperator1 = "-";
			    var seperator2 = ":";
			    var month = date.getMonth() + 1;
			    var strDate = date.getDate();
			    if (month >= 1 && month <= 9) {
			        month = "0" + month;
			    }
			    if (strDate >= 0 && strDate <= 9) {
			        strDate = "0" + strDate;
			    }
			    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
			            + " " + date.getHours() + seperator2 + date.getMinutes()
			            + seperator2 + date.getSeconds();
				
				report.value = currentdate;
				
				var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
				parent.layer.close(index); 
				
			}
					
		}
		
})
	
	
}

function checkorderedit()
{
	
	if(checknull('order-no','请输入订单编号')==false)
	{
		return false;
	}
	
	if (checknull('factory-name','请输入工厂名称') == false) {
      
        return false;
    }
	
	if (checknull('factory-addr','请输入工厂地址') == false) {
                
        return false;
    }
	
	if (checknull('manager-name','请输入负责人姓名') == false) {
       
        return false;
    }
	
	if (checknull('manager-contact','请输入负责人联系方式') == false) {
       
        return false;
    }
	
	
	
	if (checknull('main-project','请输入主营项目') == false) {
                
        return false;
    }
	
//	if (checknull('process-category','请输入主要加工类别') == false) {
//                
//        return false;
//    }
//	
//	if (checknull('process-product','请输入主要加工产品') == false) {
//        
//        return false;
//    }
	
	if (checknumber('workroom-area',1) == false) {
        
        return false;
    }
	if (checknumber('workman-number',0) == false) {
        
        return false;
    }
	
	return true;
}



function showinfo(obj,info){
	
	obj.html(info);
	
	window.setTimeout(function(){obj.html('');},3000);
	
}


function checknull(obj,msg)
{
	if ($.trim($('#'+obj).val()) == '') {
        showinfo($('#'+obj+'-err'),msg);
        $('#'+obj).focus();        
        return false;
    }
	return true;
	
}

function checknumber(obj,type)
{
	
	var regx=/^[0-9]*$/;
	
	var msg="请输入数字。";
	
	if(type==1)
		{
			regx=/^\d+(?=\.{0,1}\d+$|$)/;
			
			msg="请输入数字或小数。";
		}
	
	
	
	var value=$('#'+obj).val();
	
	if ( value==null || value== '' || !regx.test(value) ) {
        showinfo($('#'+obj+'-err'),msg);
        $('#'+obj).focus();        
        return false;
    }
	return true;
	
}

function renderForm()
{
	if($('#order-status option:selected').val()=='1')
		
		{
		
			$('#save-factory-inspection').hide();
			$('#upload-factory-photo').hide();
			$('#upload-license').hide();
		
		}
	else
		{
			$('#save-factory-inspection').show();
			$('#upload-factory-photo').show();
			$('#upload-license').show();
		}
}